import 'dart:io';

import 'package:flutter_axrjc/widget/dialog.dart';
import 'package:get/get.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:permission_handler/permission_handler.dart';

export 'package:jpush_flutter/jpush_flutter.dart';

JPush jpush = JPush();

Future<bool> hasNotifyPermission() async {
  return await Permission.notification.status == PermissionStatus.granted;
}

Future<void> initPushAndPermission() async {
  print("初始化极光推送");
  print("设置极光推送接收器");
  jpush.addEventHandler(onReceiveNotification: (map) async {
    print("接收到极光通知消息");
  }, onOpenNotification: (map) async {
    jpush.clearAllNotifications();
  }, onReceiveMessage: (map) async {
    jpush.clearAllNotifications();
    print("接收到极光消息");
    // globalController.eventBus.fire(
    //     GlobalEvent(data: map, type: GlobalEventType.refreshNotification));
  }, onInAppMessageShow: (map) async {
    print("接收到极光App内部消息");
  }, onInAppMessageClick: (map) async {
    print("接收到极光App内部被点击");
  }, onNotifyMessageUnShow: (map) async {
    print("接收到极光onNotifyMessageUnShow");
  }, onReceiveNotificationAuthorization: (map) async {
    print("接收到极光onReceiveNotificationAuthorization$map");
  });
  if (Platform.isAndroid && !await hasNotifyPermission()) {
    await showCommonDialog(
        content: "请开启通知权限，以便正常接收推送消息！",
        onConfirm: () {
          Get.back();
          jpush.openSettingsForNotification();
        },
        onCancel: () {
          Get.back();
        });
  }
  if (Platform.isIOS) {
    jpush.applyPushAuthority(const NotificationSettingsIOS());
  }
  jpush.setup(appKey: '12f632dc2e65171cfdde2bd5', debug: true);
  print("emmmmmm:中断注册号 ${await jpush.getRegistrationID()}");
  // jpush.setAlias(CommonCache.getInstance().getUserCode() ?? '');
}
